package com.furnitur.modules.portal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.furnitur.common.api.CommonResult;
import com.furnitur.common.utils.SecurityUtils;
import com.furnitur.core.entity.order.Order;
import com.furnitur.modules.portal.dto.order.OrderCreateDTO;
import com.furnitur.modules.portal.service.PortalOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/portal/order")
public class PortalOrderController {

    @Autowired
    private PortalOrderService orderService;

    @PostMapping("/create")
    public CommonResult<Order> createOrder(@Valid @RequestBody OrderCreateDTO orderDTO) {
        Order order = orderService.createOrder(orderDTO);
        return CommonResult.success(order);
    }

    @PostMapping("/pay/{orderNo}")
    public CommonResult<Void> payOrder(@PathVariable String orderNo) {
        orderService.payOrder(orderNo);
        return CommonResult.success(null);
    }

    @GetMapping("/detail/{orderNo}")
    public CommonResult<Order> getOrderDetail(@PathVariable String orderNo) {
        Order order = orderService.getOrderDetail(orderNo);
        return CommonResult.success(order);
    }

    @GetMapping("/list")
    public CommonResult<Page<Order>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer status
    ) {
        Page<Order> page = orderService.getOrderList(pageNum, pageSize, status);
        return CommonResult.success(page);
    }

    /**
     * 取消订单
     */
    @PutMapping("/cancel/{orderNo}")
    public CommonResult<?> cancelOrder(@PathVariable String orderNo) {
        orderService.cancelOrder(orderNo);
        return CommonResult.success(null);
    }

    @PostMapping("/{orderNo}/confirm")
    public CommonResult<Void> confirmReceipt(@PathVariable String orderNo) {
        // 获取当前用户ID
        Long userId = SecurityUtils.getCurrentUserId();
        orderService.confirmReceipt(orderNo, userId);
        return CommonResult.success(null);
    }
} 